﻿using Common.Base;

using CommunicationLibrary.TransportData.Enums;

using GameCommon.Manager;
using GameCommon.ManagerInterfaces;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameGraphics.Components
{
    public class Building
    {
        private Texture2D texture;

        private readonly BuildingProgressBar progressBar;

        public Building(BuildingTypeEnum buildingType, Dimension parentDimension)
        {
            this.BuildingType = buildingType;
            this.LoadTexture(this.BuildingType);
            this.SetDimension(parentDimension);

            if (buildingType != BuildingTypeEnum.None)
            {
                this.progressBar = new BuildingProgressBar(this.Dimension);
            }
        }

        public Dimension Dimension { get; private set; }

        public BuildingTypeEnum BuildingType { get; private set; }

        public void Draw(IDrawingManager manager)
        {
            if (this.texture != null)
            {
                manager.Add(this.texture, this.Dimension.Rectangle, Color.White, 50);
            }

            if (this.progressBar != null)
            {
                this.progressBar.Draw(manager);
            }
        }

        public void SetProgress(float value)
        {
            if (this.progressBar != null)
            {
                this.progressBar.Progress = value;
            }
        }

        private void LoadTexture(BuildingTypeEnum buildingType)
        {
            switch (buildingType)
            {
                case BuildingTypeEnum.Barrack:
                    this.texture = Managers.ContentManager.GetBoardTexture2D("Barrack", 1);
                    break;
                case BuildingTypeEnum.GoldMine:
                    this.texture = Managers.ContentManager.GetBoardTexture2D("GoldMine", 1);
                    break;
                case BuildingTypeEnum.Castle:
                    this.texture = Managers.ContentManager.GetBoardTexture2D("Castle", 1);
                    break;
            }
        }

        private void SetDimension(Dimension parentDimension)
        {
            this.Dimension = new Dimension();

            if (this.texture == null)
            {
                return;
            }

            var x = parentDimension.X + ((parentDimension.Width / 2) - (this.texture.Width / 2));
            var y = parentDimension.Y + ((parentDimension.Height / 2) - (this.texture.Height / 2));

            this.Dimension = new Dimension(x, y, new Size(this.texture.Width, this.texture.Height));
        }
    }
}